package drds.configuration.mvc.render;

import com.google.gson.Gson;
import drds.configuration.common.Constants;

import javax.servlet.http.HttpServletResponse;

public class JsonRender extends Render {

    private Object content;
    private Gson gson = new Gson();

    public JsonRender(Object content) {
        this.content = content;
    }

    @Override
    public void render() {

        // 默认处理
        try {
            HttpServletResponse response = getResponse();
            response.setContentType("application/json;charset=" + Constants.characterEncoding);
            response.setCharacterEncoding(Constants.characterEncoding);
            if (Constants.devMode) {
                logger.debug("输出返回响应值:" + gson.toJson(content));
            }
            gson.toJson(content, response.getWriter());
        } catch (Exception e) {
            throw new RenderException(e);
        }

    }
}
